package dk.mirasola.systemtraining.user.client.ui.createbiddingsetwizard;

import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SelectionChangeEvent;
import dk.mirasola.systemtraining.user.client.i18n.CreateBiddingSetWizardTexts;
import dk.mirasola.systemtraining.user.client.ui.DistributionFilterSelector;
import dk.mirasola.systemtraining.user.client.ui.wizard.WizardStep;
import dk.mirasola.systemtraining.user.shared.transfer.CreateBiddingSetTO;
import dk.mirasola.systemtraining.user.shared.transfer.UserDistributionFilterTO;

class DistributionFilterStep implements WizardStep {
    private CreateBiddingSetTO createBiddingSetTO;
    private CreateBiddingSetWizardTexts texts;
    private Button nextButton;
    private DistributionFilterSelector distributionFilterSelector;

    public DistributionFilterStep(final CreateBiddingSetTO createBiddingSetTO, CreateBiddingSetWizardTexts texts, final Button nextButton) {
        this.createBiddingSetTO = createBiddingSetTO;
        this.texts = texts;
        this.nextButton = nextButton;
        distributionFilterSelector = new DistributionFilterSelector();
        distributionFilterSelector.getSelectionModel().addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
            @Override
            public void onSelectionChange(SelectionChangeEvent event) {
                UserDistributionFilterTO filter = distributionFilterSelector.getSelectionModel().getSelectedObject();
                if (filter != null) {
                    createBiddingSetTO.setName(filter.getName());
                    createBiddingSetTO.setDescription(filter.getDescription());
                    createBiddingSetTO.setDistributionFilter(filter.getDistributionFilter());
                    createBiddingSetTO.getDistributions().clear();
                    nextButton.setEnabled(true);
                } else {
                    nextButton.setEnabled(false);
                }
            }
        });
    }

    public SafeHtml getProgressText() {
        return texts.distributionFilterStepProgressText();
    }

    public Widget getWidget() {
        return distributionFilterSelector;
    }

    public void activate() {
        nextButton.setEnabled(createBiddingSetTO.getDistributionFilter() != null);
        nextButton.setFocus(true);
    }
}
